package com.haha;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

public class PaladinCjqActionListener implements ActionListener {

    private final List<Integer> centerList;
    private final WinDef.RECT wowWindow = getWorldOfWarcraftWindow();
    private final Color red = new Color(255, 0, 0);
    private final Color green = new Color(0, 255, 0);
    int topAdd = 3;
    private Robot robot;

    public PaladinCjqActionListener(List<Integer> centerList) {
        this.centerList = centerList;
        try {
            robot = new Robot();
        } catch (AWTException awtException) {
            awtException.printStackTrace();
            System.out.println("初始化失败");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 第一个色块为单体开关
        int winLeft = wowWindow.left + centerList.get(0);
        int winTop = wowWindow.top + topAdd;
        Color oneTargetSwitchColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(oneTargetSwitchColor)) {
            // 单目标优先级第一的是愤怒之锤
            winLeft = wowWindow.left + centerList.get(2);
            winTop = wowWindow.top + topAdd;
            Color fnzcColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(fnzcColor)) {
                robot.keyPress(KeyEvent.VK_Q);
                robot.keyRelease(KeyEvent.VK_Q);
                return;
            }

            // 单目标优先级第二的是智慧审判
            winLeft = wowWindow.left + centerList.get(1);
            winTop = wowWindow.top + topAdd;
            Color zhspColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(zhspColor)) {
                robot.keyPress(KeyEvent.VK_1);
                robot.keyRelease(KeyEvent.VK_1);
                return;
            }

            // 单目标优先级第三的是十字军打击
            winLeft = wowWindow.left + centerList.get(3);
            winTop = wowWindow.top + topAdd;
            Color szjdjColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(szjdjColor)) {
                robot.keyPress(KeyEvent.VK_2);
                robot.keyRelease(KeyEvent.VK_2);
                return;
            }

            // 单目标优先级第四的是神圣风暴
            winLeft = wowWindow.left + centerList.get(4);
            winTop = wowWindow.top + topAdd;
            Color ssfbColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(ssfbColor)) {
                robot.keyPress(KeyEvent.VK_3);
                robot.keyRelease(KeyEvent.VK_3);
                return;
            }

            // 单目标优先级第五的是奉献
            winLeft = wowWindow.left + centerList.get(5);
            winTop = wowWindow.top + topAdd;
            Color fxColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(fxColor)) {
                robot.keyPress(KeyEvent.VK_4);
                robot.keyRelease(KeyEvent.VK_4);
                return;
            }

            // 单目标优先级第六的是驱邪术
            winLeft = wowWindow.left + centerList.get(6);
            winTop = wowWindow.top + topAdd;
            Color qxsColor = robot.getPixelColor(winLeft, winTop);
            if (green.equals(qxsColor)) {
                robot.keyPress(KeyEvent.VK_E);
                robot.keyRelease(KeyEvent.VK_E);
                return;
            }
        }
    }

    private WinDef.RECT getWorldOfWarcraftWindow() {
        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "魔兽世界");
        WinDef.RECT winRect = new WinDef.RECT();
        User32.INSTANCE.GetWindowRect(hwnd, winRect);
        return winRect;
    }
}
